<?php

/**
 * 图文素材管理
 */
namespace app\avikcm\controller;
use think\Db;
use think\facade\Request;

class MaterialArticle extends Common
{
    public function initialize()
    {
        parent::initialize();
    }


    // 列表
    public function list(){
        if(Request::isAjax()){
            $key = input('post.key');
            $type = input('post.type');
            $this->assign('testkey', $key);
            $page = input('page', 1);
            $pageSize = input('limit', config('pageSize'));
            $where [] = ['status','=',1];
            if ($key) {
                $where[] = ['title', 'like', "%" . $key . "%"];
            }
            if($type){
                $where[] = ['type','=',$type];
            }
            $paginate = ['list_rows'=>$pageSize,'page'=>$page];
            $list = db('material_article')->alias('a')->where($where)->where('title', 'like', "%" . $key . "%")->order('update_time', 'desc')->paginate($paginate)->toArray();

            if($list['data']){
                $material_type = config('material_type');
                foreach ($list['data'] as $key=>$val){
                    $list['data'][$key]['type'] = $material_type[$val['type']];
                }
            }
            return ['code'=>0,'msg'=>'成功','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        $this->assign('material_type', config('material_type'));
        return $this->fetch();
    }

    // 添加
    public function add(){
        if(Request::isAjax()){
            //构建数组
            $data = Request::except('file');
            $data['create_time'] = date("Y-m-d H:i:s",time());
            $data['update_time'] = date("Y-m-d H:i:s",time());
            $insert_id = Db::table('material_article')->insertGetId($data);
            $result['code'] = 1;
            $result['msg'] = '添加成功!';
            $result['id'] = $insert_id;
            $result['url'] = url('list');
            return $result;
        }else{
            $this->assign('material_type',config('material_type'));
            $this->assign('title', '添加图文素材');
            $this->assign('info','null');
            return $this->fetch('form');
        }
    }

    // 修改新闻
    public function edit(){
        if(Request::isAjax()){
            //构建数组
            $data = Request::except('file');
            $data['update_time'] = date("Y-m-d H:i:s");
            db('material_article')->update($data);
            $result['code'] = 1;
            $result['msg'] = '修改成功!';
            $result['url'] = url('lists');
            return $result;
        }else{
            $id = input('id');
            $info = db('material_article')->where('id',$id)->find();
            $pic = db('material_file')->where('article_id',$id)->where('type',3)->select();
            $video = db('material_file')->where('article_id',$id)->where('type',5)->find();
            $this->assign('material_type', config('material_type'));
            $this->assign('info',json_encode($info,true));
            $this->assign('baseInfo',$info);
            $this->assign('pic',$pic);
            $this->assign('video',$video);
            $this->assign('title','修改内容');
            return $this->fetch('form');
        }
    }
    // 删除
    public function del(){
        $id = input('id');
        db('material_file')->where('article_id',$id)->delete();
        $res = db('material_article')->where('id',$id)->delete();
        if($res){
            $result = ['code'=>1,'msg'=>'删除成功!'];
        }else{
            $result = ['code'=>0,'msg'=>'删除失败!'];
        }
        return $result;
    }

    // 删除图片
    public function delImg(){
        $id = input('id');
        $res = db('material_file')->where('id',$id)->delete();
        if($res){
            $result = ['code'=>1,'msg'=>'删除成功!'];
        }else{
            $result = ['code'=>0,'msg'=>'删除失败!'];
        }
        return $result;
    }

    // 批量删除
    public function delAll(){
        $map[] =array('id','in',input('param.ids/a'));
        db('material_article')->where($map)->delete();
        db('material_file')->where(['article_id', 'in', input('param.ids/a')])->delete();
        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        return $result;
    }

    //多图上传
    public function uploads()
    {
        $id = input('id');
        $type = input('type', 3);
        if ($id) {
            $fileKey = array_keys(request()->file());
            // 获取表单上传文件
            $file = request()->file($fileKey['0']);
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->validate(['ext' => 'jpg,png,gif,jpeg,mp4'])->move('uploads');
            if ($info) {
                $fileInfo = $file->getInfo();
                $path = str_replace('\\', '/', $info->getSaveName());
                $data = [
                    'article_id' => $id,
                    'title'=> $fileInfo['name'],
                    'type'=> $type,
                    'url' => '/uploads/' . $path,
                    'create_time' => date('Y-m-d H:i:s')
                ];
                if($type ==5){
                    db('material_file')->where(['article_id'=>$id, 'type'=>5])->delete();
                }
                $insert_id = db('material_file')->insertGetId($data);
                $result['code'] = 0;
                $result['msg'] = '上传成功!';
                $result["file"] = '/uploads/' . $path;
                $result['id'] = $insert_id;
                return $result;
            } else {
                // 上传失败获取错误信息
                $result['code'] = 1;
                $result['msg'] = '上传失败!';
                return $result;
            }
        } else {
            $result['code'] = 2;
            $result['msg'] = '请先保存基本信息!';
            return $result;
        }
    }

}